<template lang="html">
  <div>
    <div class="venus-list-page">
      <!--签约查询界面-->
      <div class="page-search">
        <el-form
          label-width="160px"
          style="margin: 20px 50px 20px 0px;"
          size="mini"
          id="form_report_id"
          ref="queryForm"
          :model="form.params"
        >
          <el-row>
            <!--Date-->
            <el-col v-for="(item,i) in this.form.date" :span="span_width">
              <el-form-item :label="item.label_discrible" :prop="item.css_name"
                :rules="[{required:item.data_allowblank === 'false' ? true : false}]"
                >
                <el-date-picker
                  v-model="form.params.date[i].val"
                  @focus="hideReport()"
                  type="date"
                  class="venus-input-middle venus-input-date"
                  value-format="yyyyMMdd"
                />
              </el-form-item>
            </el-col>
            <!--Text-->
            <el-col v-for="(item,i) in this.form.text" :span="span_width">
              <el-form-item :label="item.label_discrible" :prop="item.css_name">
                <el-input 
                  v-model="form.params.text[i].val"
                  @focus="hideReport()"
                  placeholder="请输入xxx"
                ></el-input>
              </el-form-item>
            </el-col>
            <!--Select-->
            <el-col v-for="(item,i) in this.form.select" :span="span_width">
              <el-form-item :label="item.label_discrible" :prop="item.css_name">
                <el-select
                  v-model="form.params.select[i].val"
                  @focus="hideReport()"
                  placeholder="请选择"
                  filterable
                  clearable
                >
                <div v-if="item.data_exeid != null && item.data_exeid != ''">
                  <el-option
                    v-for="item in select_data[item.data_exeid]"
                    :key="item.key"
                    :label="item.value"
                    :value="item.key"
                  />
                </div>
                <div v-if="item.data_dict != null && item.data_dict != ''">
                  <el-option
                    v-for="(key,value) in select_data[item.data_dict]"
                    :key="value"
                    :label="key"
                    :value="value"
                  />
                </div>
               </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <div style="text-align: center;" >
            <el-button type="success" @click="selectdata('queryForm')" size="mini">查询</el-button>
            <el-button type="info" size="mini" @click="test">重置</el-button>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>
import request from '@/utils/request'
import * as kk from '@/utils/k-utils'
  export default {
    //name:'index4',
    data() {
      return {
        select_data:{
        },
        menuid:'M84Z01',//M84Z01|M84Z10
        condition:'',
        xmladdress:'',
        span_width:8,//查询条件标签长度
        form_rules:{

        },
        form:{
          date:[],
          text:[],
          select:[],
          params:{
            date:[],
            text:[],
            select:[]
          }
        },
        form_params:{}
      };
    },
    mounted:function(){
      request({
        loading: true,
        url: "data/getReportCondition.json",
        method: 'post',
        params: {"menuid":this.menuid}//M812SP005|M84FZ002|M84201|
      }).then(rep =>{
        var condition_Arr=rep.returndata.rows;
        if(condition_Arr.length < 3){
          this.span_width=12;
        }
        for (let i = 0; i < condition_Arr.length; i++) {
          const element = condition_Arr[i];
          // this.form_rules[element.css_name]=new Array();
          // this.form_rules[element.css_name].push({required:element.data_allowblank === "false" ? true : false,message:"请输入"+element.label_discrible});
          if(element.css_class == "k-field-date"){
            this.form.date.push(element);
            this.form.params.date.push({val:""});
          }else if(element.css_class == "k-field-text"){
            this.form.text.push(element);
            this.form.params.text.push({val:""});
          }else if(element.css_class == "k-field-select"){
            this.select_data[element.data_exeid]=new Array();
            this.form.params.select.push({val:""});
            //数据字典
            if(element.data_dict != null && element.data_dict != ""){
              kk.k_load_report_dict(element.data_dict).then(rep=>{
                this.select_data[element.data_dict]=rep;
                this.form.select.push(element);
              });
            }
            //exeid
            if(element.data_exeid != null && element.data_exeid != ""){
              var key_val_arr=element.data_display_field.split(",");
              kk.k_load_exeid("data",element.data_exeid).then(rep=>{
                let exeid_val_arr=[];
                rep.rows.forEach(data_element => {
                  this.select_data[element.data_exeid].push({key:data_element[key_val_arr[0]],value:data_element[key_val_arr[1]]});
                });
                this.form.select.push(element);
              });
            }
          }
        }
      });
      
    },
    activated:function(){
        this.showReport();
        setTimeout(() => {
          $("#report_data_id").css("top",($("#form_report_id").height()+130)+"px");
          this.OnReady('AF');
        }, 500);
    },
    deactivated:function(){
      $("#report_data_id").hide();
    },
    methods: {
      test(){
        
      },
      get_random(){
        var d = new Date();
        var str = '';
        str += d.getFullYear();
        str  += d.getMonth() + 1;
        str  += d.getDate();
        str += d.getHours(); 
        str  += d.getMinutes(); 
        str+= d.getSeconds(); 
          var suijishu =	(parseInt(1000*Math.random())).toString();
          var results = str+suijishu;
        return results;
      },
      getRootPath(){
          //获取主机地址之后的目录，如： uimcardprj/share/meun.jsp
          //var pathName=window.document.location.pathname;
          //获取当前访问的根目录
          //var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);
          //return projectName;
          return "http://zg.stress.kkws.cn:8889";
      },
      setXmlUrl(exeid) {
        var str = exeid;
        for ( var key in this.form_params) {
          str += "&" + key + "=" + encodeURI(encodeURI(this.form_params[key]));
        }
        return this.getRootPath()+"/data/getResultData.json?exeid=" + str+"&id="+this.get_random();
      },
      selectdata(formName){
        this.showReport();
        setTimeout(() => {
          var rw =[],datas = [],exeids = [];
          kk.k_load_exeid("data","M8430EQ004",{for_table:this.menuid}).then(rep=>{
            if (rep.rows.length > 0) {
              rw = rep.rows;
            }
            for(var i = 0;i < rw.length;i++){
              datas.push(rw[i].ds);
              exeids.push(rw[i].exeid);
            }
            myfunc(AF,"Build", this.xmladdress);
            this.getFormParams();
            for(var i=0;i<datas.length;i++){
              myfunc(AF,"SetSource", datas[i]+" \r\n "+this.setXmlUrl(exeids[i]));
            }
            myfunc(AF,"Calc", "");
            //计算后，重新复制给xmladdress，否则报表隐藏后不会再回显原数据
            this.xmladdress=myfunc(AF,"GetFileXML","isSaveCalculateResult=true");
          });
        }, 500);
        // this.$refs[formName].validate((valid) => {
        //   if (valid) {

        //   } else {
        //     //console.log('error submit!!');
        //     return false;
        //   }
        // });
      },
      OnReady(id) {
        if(this.xmladdress == ""){
          request({
            loading: true,
            url: "data/getxml.json",
            method: 'post',
            params: {"for_table":this.menuid}//M812SP005|M84FZ002|M84201|
          }).then(rep =>{
            this.xmladdress=rep.returndata.xml_info;
            myfunc(AF,"Build", this.xmladdress);
            myfunc(AF,"Swkrntpomzqa", "1, 2, 4d, 8, 16, 32, 128");
          });
        }else{
          myfunc(AF,"Build", this.xmladdress);
          myfunc(AF,"Swkrntpomzqa", "1, 2, 4d, 8, 16, 32, 128");
        }
      },
      getFormParams() {
        //var report_condition_div = document.querySelector('#report_condition_div');
        // console.log(this.$refs.report_condition_div.style.height);
        //报表
        //this.OnReady(1);
        //获取参数
        for (let i = 0; i < this.form.date.length; i++) {
          const element = this.form.date[i];
          this.form_params[element.css_name]=this.form.params.date[i].val;
        }
        for (let i = 0; i < this.form.text.length; i++) {
          const element = this.form.text[i];
          this.form_params[element.css_name]=this.form.params.text[i].val;
        }
        for (let i = 0; i < this.form.select.length; i++) {
          const element = this.form.select[i];
          this.form_params[element.css_name]=this.form.params.select[i].val;
        }
      },
      hideReport(){
        $("#report_data_id").hide();
      },
      showReport(){
        $("#report_data_id").show();
      }
    }
  }
</script>